package com.jadefashion.controller;

import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.CheckGi;
import com.jadefashion.pojo.CheckGroup;
import com.jadefashion.pojo.CheckItem;
import com.jadefashion.pojo.MulitObjectBean;
import com.jadefashion.service.CheckGroupService;
import com.jadefashion.service.CheckitemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    //添加一条信息
    @RequestMapping("addCheckGroup.do")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Object addCheckGroup(@RequestBody MulitObjectBean mulitObjectBean){
        Result result= null;
        try {
            checkGroupService.addCheckGroup(mulitObjectBean);
            result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping("updateCheckGroup.do")//updateCheckGroup
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Object updateCheckGroup(@RequestBody MulitObjectBean mulitObjectBean){
        Result result= null;
        try {
            checkGroupService.updateCheckGroup(mulitObjectBean);
            result = new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }


    @RequestMapping("findPage.do")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Object findPage(@RequestBody QueryPageBean queryPageBean){
        Result result= null;
        try {
            Map<String,Object> map= checkGroupService.findPage(queryPageBean);
            result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping("deleteCheckGroupById.do")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Object deleteCheckGroupById(@RequestParam("id") Integer id){
        try {
            checkGroupService.deleteCheckitemById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("findCheckItemIds.do")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Object findCheckItemIds(@RequestParam("id")Integer id){
        Result result= null;
        try {
            Integer[] ids= checkGroupService.findCheckItemIdsByGroupId(id);
            result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,ids);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping("findAll.do")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Object findAll(){
        Result result= null;
        try {
            List<CheckGroup> list= checkGroupService.findAll();
            result = new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }
}
